using System;
using UnityEngine;

public class CluifyManager
{
	public static int RUNTIME_PERMISSIONS_MIN_SDK_LEVEL = 23;

	private static bool isInitialized;

	public static void InitializeCluify()
	{
		UnityEngine.Debug.Log("On Initialize Cluify");
		if (!isInitialized)
		{
			if (GetSDKLevel() < RUNTIME_PERMISSIONS_MIN_SDK_LEVEL)
			{
				UnityEngine.Debug.Log("Starting cluify");
				StartCluify();
			}
			else
			{
				UnityEngine.Debug.Log("Requesting permissions");
				RequestCluifyPermissions();
			}
			isInitialized = true;
		}
	}

	public static void StartCluify()
	{
		using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.cluify.android.CluifyManager"))
		{
			androidJavaClass.CallStatic("start", GetActivity());
		}
	}

	public static void RequestCluifyPermissions()
	{
		using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.cluify.android.CluifyManager"))
		{
			androidJavaClass.CallStatic("requestPermissions", GetActivity());
		}
	}

	public static AndroidJavaObject GetActivity()
	{
		return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
	}

	public static int GetSDKLevel()
	{
		IntPtr clazz = AndroidJNI.FindClass("android/os/Build$VERSION");
		IntPtr staticFieldID = AndroidJNI.GetStaticFieldID(clazz, "SDK_INT", "I");
		return AndroidJNI.GetStaticIntField(clazz, staticFieldID);
	}
}
